//
//  GoodsListCell.m
//  RRBI
//
//  Created by kuyuZJ on 16/7/5.
//
//

#import "GoodsListCell.h"
#import "TopLeftLabe.h"
#import <SDWebImage/UIImageView+WebCache.h>


@interface GoodsListCell ()

@property(nonatomic,strong) UIImageView* photo;
@property(nonatomic,strong) UIView* middleView;
@property(nonatomic,strong) TopLeftLabe* labelTitle;
@property(nonatomic,strong) UILabel* labelPrice;
@property(nonatomic,strong) UIView* rightView;
@property(nonatomic,strong) UIButton* btnpPrice;
@property(nonatomic,strong) UIButton* btnStatus;
@property(nonatomic,strong) UIImageView* line;

@end

@implementation GoodsListCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self.contentView addSubview:self.photo];
    [self.contentView addSubview:self.middleView];
    [self.middleView addSubview:self.labelTitle];
    [self.middleView addSubview:self.labelPrice];
    [self.contentView addSubview:self.rightView];
    [self.rightView addSubview:self.btnpPrice];
    [self.rightView addSubview:self.btnStatus];
    [self.contentView addSubview:self.line];
}

-(void)layoutConstraints{
    
    self.photo.translatesAutoresizingMaskIntoConstraints = NO;
    self.middleView.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelTitle.translatesAutoresizingMaskIntoConstraints = NO;
    self.labelPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.rightView.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnpPrice.translatesAutoresizingMaskIntoConstraints = NO;
    self.btnStatus.translatesAutoresizingMaskIntoConstraints = NO;
    self.line.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.photo addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.f]];
    [self.photo addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.photo attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:10.f]];
    
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:50.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-10.f]];
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.middleView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:10.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.middleView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.photo attribute:NSLayoutAttributeRight multiplier:1.0 constant:5.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.middleView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-10.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.middleView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-5.f]];
    
    [self.labelPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:15.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPrice attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelPrice attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.labelPrice attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.middleView addConstraint:[NSLayoutConstraint constraintWithItem:self.labelTitle attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.middleView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.f]];
    
    [self.btnpPrice addConstraint:[NSLayoutConstraint constraintWithItem:self.btnpPrice attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnpPrice attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnpPrice attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnpPrice attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.btnStatus addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:20.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.rightView addConstraint:[NSLayoutConstraint constraintWithItem:self.btnStatus attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.rightView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    
    [self.line addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:1.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.line attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.f]];
    
}

#pragma mark =====================================================  SEL
-(IBAction)updatePriceTouch:(id)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(updateGoodsPrice:)]){
        [self.delegate updateGoodsPrice:self.entity];
    }
}

-(IBAction)pushlishTouch:(id)sender{
    if(self.delegate && [self.delegate respondsToSelector:@selector(publishGoods:)]){
        [self.delegate publishGoods:self.entity];
    }
}

#pragma mark =====================================================  property package
-(void)setEntity:(MGoods *)entity{
    if(entity){
        _entity = entity;
        [self.photo sd_setImageWithURL:[NSURL URLWithString:entity.thumbnail] placeholderImage:[UIImage imageNamed: @"Icon-60"]];
        self.labelTitle.frame = CGRectMake(0, 0, SCREEN_WIDTH-140, CGRectGetHeight(self.frame)-35);
        self.labelTitle.text = entity.goodsName;
        self.labelPrice.text = [NSString stringWithFormat: @"结算价:%@ 销售价:%@",entity.checkPrice,entity.price];
        if([entity.status integerValue] == 0){
            self.btnStatus.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"bg-store-btn-publish"]];
            [self.btnStatus setTitle: @"已上架" forState:UIControlStateNormal];
        }else{
            self.btnStatus.backgroundColor = [UIColor colorWithRed:181/255.f green:181/255.f blue:182/255.f alpha:1.0];
            [self.btnStatus setTitle: @"已下架" forState:UIControlStateNormal];
        }
        
    }
}
-(UIImageView *)photo{
    if(!_photo){
        _photo = [[UIImageView alloc]init];
        _photo.layer.borderColor = theme_line_color.CGColor;
        _photo.layer.borderWidth = 1.f;
    }
    return _photo;
}
-(UIView *)middleView{
    if(!_middleView){
        _middleView = [[UIView alloc]init];
    }
    return _middleView;
}

-(TopLeftLabe *)labelTitle{
    if(!_labelTitle){
        _labelTitle = [[TopLeftLabe alloc]init];
        _labelTitle.numberOfLines = 0;
        _labelTitle.textColor = [UIColor colorWithRed:60/255.f green:60/255.f blue:60/255.f alpha:1.0];
        _labelTitle.font = [UIFont systemFontOfSize:14.f];
        [_labelTitle setContentMode:UIViewContentModeTop];
    }
    return _labelTitle;
}

-(UILabel *)labelPrice{
    if(!_labelPrice){
        _labelPrice = [[UILabel alloc]init];
        _labelPrice.textColor = [UIColor colorWithRed:125/255.f green:125/255.f blue:125/255.f alpha:1.f];
        _labelPrice.font = [UIFont systemFontOfSize:12.f];
    }
    return _labelPrice;
}

-(UIView *)rightView{
    if(!_rightView){
        _rightView = [[UIView alloc]init];
    }
    return _rightView;
}

-(UIButton *)btnpPrice{
    if(!_btnpPrice){
        _btnpPrice = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnpPrice setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnpPrice setTitle: @"修改价格" forState:UIControlStateNormal];
        _btnpPrice.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"bg-store-btn-update"]];
        _btnpPrice.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnpPrice addTarget:self action:@selector(updatePriceTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnpPrice;
}

-(UIButton *)btnStatus{
    if(!_btnStatus){
        _btnStatus = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnStatus setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnStatus setTitle: @"已上架" forState:UIControlStateNormal];
        _btnStatus.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: @"bg-store-btn-publish"]];
        _btnStatus.titleLabel.font = [UIFont systemFontOfSize:14.f];
        [_btnStatus addTarget:self action:@selector(pushlishTouch:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnStatus;
}

-(UIImageView *)line{
    if(!_line){
        _line = [[UIImageView alloc]init];
        _line.backgroundColor = theme_line_color;
    }
    return _line;
}





@end
